#ifndef TCPCLIENT_H
#define TCPCLIENT_H

#include <QFile>
#include <QObject>
#include <QTcpSocket>
#include <protocol_define.h>

class TcpClient : public QObject
{
    Q_OBJECT
public:
    explicit TcpClient(QObject *parent = nullptr);
public slots:
    void init();

    void clientConnectTo(QString ip , QString port);

    void sendData(const QString &);

    void sendFile(const QString & filepath,int seek = 0 );

    void closeConnect();
signals:
    void signReadyRead(TcpProtocol);

    void signConnected();

    void signDisconnected();

    void signDisplayError(QAbstractSocket::SocketError);
public slots:

    void slotReadyRead();

    void slotConnected();

    void slotDisconnected();

    void displayError(QAbstractSocket::SocketError);

private:
    void save2File(TcpProtocol);

    void sendReadFileData(TcpProtocol , int num = 0);

public slots:
    void startTransfer();

    void updateClientProgress(qint64);

private slots:
    void updateServerProgress();
private:
    QTcpSocket* m_socket;

    QByteArray m_buffer;

    QString     m_filePath;

    QFile       m_file;

    QString     m_saveFileName;

    QTcpSocket* m_filesocket;

    QFile *     m_localFile;
    qint64      m_totalBytes;
    qint64      m_bytesWritten;
    qint64      m_bytesToWrite;
    qint64      m_payloadSize;
    QString     m_fileName;
    QByteArray  m_outBlock;

    qint64      m_recvtotalBytes;
    qint64      m_recvbytesReceived;
    qint64      m_recvfileNameSize;
    QString     m_recvfileName;
    QFile *     m_recvlocalFile;
    QByteArray  m_recvinBlock;

};

#endif // TCPCLIENT_H
